package com.health.viewscontrols.photo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.health.viewscontrols.NovaActivity;
import com.health.viewscontrols.NovaApplication;
import com.health.R;
import com.health.db.DbManager;
import com.health.tablebean.ARCHIVE_BASEINFO;
import com.health.tablebean.HD_PERSONAL_HEALTH_PHOTO;
import com.health.tools.GlobalConts;
import com.nova.tools.ImageUtil;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 拍照   照片数据库存储的是二进制文件
 * 
 * @author nova
 *
 */
public class PhotoMainActivity extends NovaActivity {
	private Button photo;// 拍照
	private Button btn_back;
	@SuppressWarnings("unused")
	private Button buttonUp;// 保存

	private String str_imgpath;
	private ImageView img_photo;
	private DbManager dbmanger = null;
	public ARCHIVE_BASEINFO userInfo;

	private byte[] sz_photo = null;
	TextView title;
	private int from_data = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.photo);
		dbmanger = ((NovaApplication) getApplication()).getDbManger();
		userInfo = NovaApplication.getUserInfo();
		// 设置拍照形状
		GlobalConts.photo_flag="rect";
		findViews();
		setListeners();
		str_imgpath = null;
		setData();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (requestCode == 100 && resultCode == RESULT_OK) {
			String str_imgpath11 = Environment.getExternalStorageDirectory() + File.separator + "camera.jpg";

			Intent intent = new Intent(PhotoMainActivity.this, ClipActivity.class);
			intent.putExtra("str_img", str_imgpath11);
			startActivityForResult(intent, 0);

		} else if (requestCode == 0) {
			str_imgpath = data.getStringExtra("path");
			if (!TextUtils.isEmpty(str_imgpath)) {
				sz_photo = ImageUtil.getBitmapByte(getLoacalBitmap(str_imgpath));

				img_photo.setImageBitmap(getLoacalBitmap(str_imgpath));
			}
		}

		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void findViews() {
		title = (TextView) findViewById(R.id.title);
		title.setText("拍取照片");
		photo = (Button) findViewById(R.id.photo);
		buttonUp = (Button) findViewById(R.id.btn_up);
		btn_back = (Button) findViewById(R.id.btn_back);
		img_photo = (ImageView) findViewById(R.id.img_photo);
	}

	@Override
	public void setListeners() {
		btn_back.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
		photo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
						Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
						intent.putExtra(MediaStore.EXTRA_OUTPUT,
								Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "camera.jpg")));
						str_imgpath = new File(Environment.getExternalStorageDirectory(), "camera.jpg").getPath();
						intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
						startActivityForResult(intent, 100);
					} else {
						Toast.makeText(PhotoMainActivity.this, "SD卡不存在，不能拍照", Toast.LENGTH_SHORT).show();
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		// 保存
		buttonUp.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				savePhotoData();
			}
		});
	}

	/**
	 * @param url
	 * @return
	 */
	public static Bitmap getLoacalBitmap(String url) {
		try {
			FileInputStream fis = new FileInputStream(url);
			return BitmapFactory.decodeStream(fis);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 保存数据
	 * 
	 * @author 王婷婷
	 * @date 2016年1月11日 下午4:39:34
	 */
	private void savePhotoData() {
		HD_PERSONAL_HEALTH_PHOTO ques = new HD_PERSONAL_HEALTH_PHOTO();
		ques.setCARDID(userInfo.getIDCARD());
		if (sz_photo != null) {
			Log.w("eeeeeeeeeeeee", Arrays.toString(sz_photo));
			ques.setPHOTO(sz_photo);
		} else {
			showTextDialog("请拍照后再保存");
			return;
		}
		ques.setCREATETIME(new Date());
		ques.setCREATEORG(userInfo.getCREATEORG());
		ques.setCREATER(userInfo.getCREATED_BY());
		int saveFlag=-1;
		if (from_data == 1) {
			// fdbmanger.updata(ques, "CARDID='" + userInfo.getIDCARD() + "' "
			// );
			saveFlag=dbmanger.updatePhoto(ques);
		} else {
			saveFlag= dbmanger.addPhoto(ques);
			// fdbmanger.save(ques);
			
		}
		
		if(saveFlag>0){
			showTextDialog("保存成功");
		}else{
			showTextDialog("保存失败");
		}
	}

	/**
	 * 设置数据
	 * 
	 * @author 王婷婷
	 * @date 2016年1月12日 上午9:59:28
	 */
	private void setData() {
		// List<HD_PERSONAL_HEALTH_PHOTO> queses =
		// fdbmanger.findAllByWhere(HD_PERSONAL_HEALTH_PHOTO.class,
		// "CARDID='" + userInfo.getIDCARD() + "'");
		List<HD_PERSONAL_HEALTH_PHOTO> queses = dbmanger.selectDataPHOTO(userInfo.getIDCARD());
		if (queses.size() > 0) {
			from_data = 1;
			HD_PERSONAL_HEALTH_PHOTO ques = queses.get(0);
			if (ques.getPHOTO() != null) {
				Log.w("dddddddddddd", Arrays.toString(ques.getPHOTO()));
				sz_photo = ques.getPHOTO();
				if (sz_photo != null) {

					Bitmap bitmap = BitmapFactory.decodeByteArray(sz_photo, 0, sz_photo.length);
					if (GlobalConts.photo_flag.equals("circle")) {

						img_photo.setImageBitmap(ImageUtil.toRoundBitmap(bitmap));
					} else {
						img_photo.setImageBitmap(bitmap);
					}
				}
			}

		} else {
			from_data = 0;
		}

	}
}
